using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using B.MESUI.Login.WareHouseManage.成品.Model;

namespace B.MESUI.Login.WareHouseManage.Services
{
    public class MadeUpArticlesService
    {
        private static readonly HttpClient _httpClient = new HttpClient();
        private const string BaseUrl = "http://localhost:5031/api";

        public static async Task<ApiResult> CreateMadeUpArticlesAsync(MadeUpArticlesModel model)
        {
            try
            {
                var json = JsonConvert.SerializeObject(model);
                var content = new StringContent(json, Encoding.UTF8, "application/json");
                
                var response = await _httpClient.PostAsync($"{BaseUrl}/Made_UpArticles/CreateMade_UpArticlesModel", content);
                
                if (response.IsSuccessStatusCode)
                {
                    var responseContent = await response.Content.ReadAsStringAsync();
                    return JsonConvert.DeserializeObject<ApiResult>(responseContent);
                }
                else
                {
                    return new ApiResult
                    {
                        Code = (int)response.StatusCode,
                        Msg = $"HTTP错误: {response.StatusCode}",
                        Data = null
                    };
                }
            }
            catch (Exception ex)
            {
                return new ApiResult
                {
                    Code = -1,
                    Msg = $"网络异常: {ex.Message}",
                    Data = null
                };
            }
        }
    }

    public class ApiResult
    {
        public int Code { get; set; }
        public string Msg { get; set; }
        public object Data { get; set; }
    }
} 